from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from .serializers import AreaSerializer, SubAreaSerializer


# Create your views here.


class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """省市区划信息"""
    pagination_class = None  # 禁用分页

    def get_queryset(self):
        """提供查询数据"""
        if self.action == 'list':
            return Area.objects.filter(parent=None)

        return Area.objects.all()

    def get_serializer_class(self):
        """提供序列化器"""
        if self.action == 'list':
            return AreaSerializer

        return SubAreaSerializer
